package it.maggioni.struts2.actions;

import it.maggioni.struts2.model.User;
import it.maggioni.struts2.services.LoginService;

import org.apache.commons.lang3.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 4205984719423955355L;
	
	private User user = new User();
	
	public void validate() {
		if (StringUtils.isEmpty(user.getUsername())) {
			addFieldError("user.username", "Devi inserire un nome utente");
		}
		if (StringUtils.isEmpty(user.getPassword())) {
			addFieldError("user.password", "Devi inserire una password");
		}
	}

	public String execute() {
		LoginService loginServices = new LoginService();
		if (loginServices.verifyLogin(user)) {
			return SUCCESS;
		}
		return LOGIN;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	
}
